package com.operation.service.impl;

import com.operation.beans.*;
import com.operation.dao.HandleInfoMapper;
import com.operation.dao.OneLevelDirectoryMapper;
import com.operation.dao.UserInfoMapper;
import com.operation.dao.WorkOrderMapper;
import com.operation.entity.HandleInfo;
import com.operation.entity.UserInfo;
import com.operation.entity.WorkOrder;
import com.operation.service.LevelDirectoryService;
import com.operation.service.WorkOrderService;
import com.operation.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class WorkOrderServiceImpl extends State implements WorkOrderService{
    @Autowired
    WorkOrderMapper workOrderMapper;
    @Autowired
    HandleInfoMapper handleInfoMapper;
    @Autowired
    UserInfoMapper userInfoMapper;
    @Autowired
    OneLevelDirectoryMapper oneLevelDirectoryMapper;

    @Override
    @Transactional
    public Result addWorkOrder(WorkOrder workOrder) {
        String a = null;
        Change change = new Change();
        Supplement supplement = new Supplement();
        Calendar now = Calendar.getInstance();
        workOrder.setUploadTime(now.getTime());
        workOrder.setDetails(oneLevelDirectoryMapper.selectOne(workOrder.getType()).getOne());
        //为1表示分配，接收,为0表示未归档
        workOrder.setIsDistribution(1);
        workOrder.setIsReceive(1);
        workOrder.setIsComplete(0);
        workOrder.setIsPigeonhole(0);
        workOrder.setIscomment(0);
        workOrder.setDistributionTime(now.getTime());
        workOrder.setCompleteTime(now.getTime());
        workOrder.setUserAccount(Long.valueOf(workOrder.getUserAccount()));
        //设置工单号
        a = String.valueOf(now.get(Calendar.YEAR)
                + change.method(now.get(Calendar.MONTH) + 1)
                + change.method(now.get(Calendar.DAY_OF_MONTH))
                + change.method(now.get(Calendar.HOUR_OF_DAY))
                + change.method(now.get(Calendar.MINUTE))
                + supplement.method(workOrderMapper.aFewSingle().getType() + 1));
        workOrder.setJobnumber(a);
        workOrderMapper.insertSelective(workOrder);
        //处理表
        HandleInfo handleInfo = new HandleInfo();
        handleInfo.setJobnumber(a);
        handleInfo.setReceiveOne(Math.toIntExact(userInfoMapper.findUserByType(workOrder.getType()).getUserAccount()));
        handleInfoMapper.insert(handleInfo);
        return ResultUtil.success(userInfoMapper.findUserByType(workOrder.getType()), 0);
    }

    @Override
    public int addWorkOrderPhoto(WorkOrder workOrder) {
        return workOrderMapper.updateImg(workOrder);
    }

    @Override
    public Result queryWorkOrderInformation(WorkOrder workOrder) {
        return ResultUtil.success(workOrderMapper.findWorkOrderInfo(workOrder),0);
    }

    @Override
    public Result deleteWorkOrder(WorkOrder workOrder) {
        String myState = null;
        HandleInfo handleInfo = new HandleInfo();
        handleInfo.setJobnumber(workOrder.getJobnumber());
        int j = handleInfoMapper.deleteHandleInfo(handleInfo);
        int i = workOrderMapper.deleteWorkOrder(workOrder);
        if (i == 1 && j == 1){
            myState =s9;
        }else if (i == -1 && j == 1){
            myState =s10;
        }else if (i == 1 && j == -1 ){
            myState =s11;
        }else if (i == -1 && j == -1){
            myState = s12;
        }
        return ResultUtil.success(myState,0);
    }

    @Override
    public Result queryMonthWorkOrder(Performance performance) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, performance.getYear());

        Date currYearFirst = calendar.getTime();
        calendar.set(Calendar.YEAR, performance.getYear());
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        Date currYearLast = calendar.getTime();
        performance.setStartTime(currYearFirst);
        performance.setEndTime(currYearLast);
        List<WorkOrderAllInfo> list = workOrderMapper.byYear(performance);
        return ResultUtil.success(DataUtil.wrapperData(list),0);
    }

    @Override
    public Result queryItMonthWorkOrder(Performance performance) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, performance.getYear());
        Date currYearFirst = calendar.getTime();
        calendar.set(Calendar.YEAR, performance.getYear());
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        Date currYearLast = calendar.getTime();
        performance.setStartTime(currYearFirst);
        performance.setEndTime(currYearLast);
        List<WorkOrderAllInfo> list = workOrderMapper.byPeople(performance);
        return ResultUtil.success(ITDataUtil.wrapperData(list),0);
    }

    @Override
    public List<WorkOrderAllInfo> findComplaintAndStar() {
        return workOrderMapper.findComplaintAndStar();
    }

    @Override
    public int deleteComplaint(String jobNumber) {
        return workOrderMapper.deleteComplaint(jobNumber);
    }

    @Override
    public List<WorkOrderAllInfo> findComplaintAndIspigeonhole() {
        return workOrderMapper.findComplaintAndIspigeonhole();
    }

    @Override
    public String isUnderway(Integer userAccount) {
        if (workOrderMapper.isUnderway(userAccount)==null){
            return "1";
        }else {
            return "0";
        }
    }

    @Override
    public WorkOrder aFewSingle() {
        return workOrderMapper.aFewSingle();
    }

    @Override
    public WorkOrder isComment(String jobNumber) {
        return workOrderMapper.isComment(jobNumber);
    }

    @Override
    public WorkOrder isComplaint(String jobNumber) {
        return workOrderMapper.isComplaint(jobNumber);
    }

    @Override
    public Result byDetails(Performance performance) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, performance.getYear());

        Date currYearFirst = calendar.getTime();
        calendar.set(Calendar.YEAR, performance.getYear());
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        Date currYearLast = calendar.getTime();
        performance.setStartTime(currYearFirst);
        performance.setEndTime(currYearLast);
        List<WorkOrderAllInfo> list = workOrderMapper.byDetails(performance);

        return ResultUtil.success(DataUtil.wrapperData(list),0);
    }
}
